package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.mapper.UserRoleMapper;
import com.zhentao.pojo.SysRole;
import com.zhentao.pojo.SysUser;
import com.zhentao.pojo.SysUserRole;
import com.zhentao.service.SysRoleService;
import com.zhentao.service.SysUserRoleService;
import com.zhentao.service.SysUserService;
import com.zhentao.service.UserService;
import com.zhentao.vo.ResultUser;
import com.zhentao.vo.RoleVO;
import com.zhentao.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务实现类
 * 实现用户信息查询和角色查询等功能
 *
 * @author zhentao
 * @since 2024-01-01
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    /**
     * 用户基础信息服务
     */
    private final SysUserService sysUserService;
    private final SysUserRoleService sysUserRoleService;
    private final UserRoleMapper userRoleMapper;

    /**
     * 获取用户详细信息，包括基本信息和角色信息
     *
     * @param id 用户ID
     * @return 返回用户信息VO对象，包含用户基本信息和角色列表
     * @throws RuntimeException 当用户不存在时抛出异常
     */
    @Override
    public UserInfoVO getUserInfo(Long id) {
        // 1. 获取用户基本信息
        SysUser sysUser = sysUserService.getById(id);
        if (sysUser == null) {
            throw new RuntimeException("用户不存在");
        }
        String createTime = sysUser.getCreateTime();
        // 2. 转换为VO对象并设置角色信息
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(sysUser, userInfoVO);
        userInfoVO.setRoles(getUserRoles(id));
        userInfoVO.setAvatar(sysUser.getAvatar());
        userInfoVO.setName(sysUser.getName());
        userInfoVO.setCreateTime(createTime);
        return userInfoVO;
    }

    /**
     * 获取用户的角色列表
     *
     * @param userId 用户ID
     * @return 返回角色VO对象列表
     * @throws RuntimeException 当用户不存在时抛出异常
     */
    @Override
    public List<RoleVO> getUserRoles(Long userId) {
        return userRoleMapper.getUserRoles(userId);
    }
    //查询所有用户信息
    @Override
    public ResultUser findAllUser(Long id) {
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        //获取中间表
        SysUserRole sysUserRole = sysUserRoleService.getOne(wrapper);
        //获取角色id
        Long roleId = sysUserRole.getRoleId();
        //判断是否是管理员
        if(roleId==1){
            //返回用户信息
            List<SysUser> list = sysUserService.list();
            for (SysUser sysUser : list) {
                //不返回密码
                sysUser.setPassword(null);
                System.err.println(sysUser.getUpdateTime());
            }
            return new ResultUser(200,"查询成功",list);
        }else {
            return new ResultUser(401,"查询失败,无权限",null);
        }

    }

    @Override
    public UserInfoVO getUserInfoByUsername(String username) {
        return null;
    }


}
